using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace XNAGameEngine.GameState
{
    /// <summary>
    /// A basic type of screen transition, fading the entire screen to a defined color (default black)
    /// </summary>
    public sealed class GSTFadeToWhite : GameStateTransition
    {
        #region local fields

        //the texture to use for the transition
        private Texture2D tex;

        /// <summary>
        /// Get or set the transition color
        /// </summary>
        private Color _fadeColor;

        #endregion

        #region constructors

        /// <summary>
        /// Constructor to setup the transition.
        /// Sets the fade color to black by default
        /// </summary>
        /// <param name="graphicsDevice">the graphics device from the GameEngine</param>
        /// <param name="spriteBatch">the spritebatch from the GameEngine</param>
        public GSTFadeToWhite(GraphicsDevice graphicsDevice, SpriteBatch spriteBatch)
            : this(graphicsDevice, spriteBatch, Color.White)
        { }

        /// <summary>
        /// Constructor to setup the transition
        /// </summary>
        /// <param name="graphicsDevice">the graphics device from the GameEngine</param>
        /// <param name="spriteBatch">the spritebatch from the GameEngine</param>
        /// <param name="color">the fadeout/fadein color</param>
        private GSTFadeToWhite(GraphicsDevice graphicsDevice, SpriteBatch spriteBatch, Color color)
            : base(graphicsDevice, spriteBatch)
        {
            _fadeColor = color;
            tex = new Texture2D(_graphicsDevice, 1, 1);
            tex.SetData<Color>(new Color[] { Color.White });
            _graphicsDevice = graphicsDevice;
            _spriteBatch = spriteBatch;
            _transitionName = "FadeToWhite";
        }

        #endregion

        #region GameStateTransition methods

        /// <summary>
        /// Does the drawing of the fade screen. Draws a single pixel and stretches it across the entire screen.
        /// The alpha value is used for the transparency and is set every 
        /// </summary>
        protected override void Draw()
        {
            _spriteBatch.Begin();
            _spriteBatch.Draw(tex,
                              new Rectangle(0, 0, _graphicsDevice.Viewport.Width, _graphicsDevice.Viewport.Height),
                              new Color(_fadeColor, _alpha));
            _spriteBatch.End();
        }

        #endregion
    }
}